﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Entity;
using DAL;
using Models;
using System.Data.Linq;
using System.Linq.Expressions;
namespace BAL
{
    public class Assess : BaseBAL<AssessInfo, Models.Assess>
    {
        /// <summary>
        /// 取得一条AssessInfo
        /// </summary>
        /// <param name="InputId">信息ID</param>
        /// <returns></returns>
        public static AssessInfo GetSingleInfo(int InputId)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return TransT2ToT1(db.GetTable<Models.Assess>().Single(t => t.Id == InputId));
            }
        }

        /// <summary>
        /// 取得全部列表
        /// </summary>
        /// <returns></returns>
        public static IList<AssessInfo> GetList()
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return db.GetTable<Models.Assess>().Select(t => TransT2ToT1(t)).ToList();
            }
        }

        /// <summary>
        /// 保存信息
        /// </summary>
        /// <param name="InstInfo">如果id=0则新增>0时编辑</param>
        /// <returns></returns>
        public static AssessInfo Save(AssessInfo InstInfo)
        {
            if (InstInfo.Id > 0)
            {
                Edit(InstInfo);
                return InstInfo;
            }
            else
            {
                return Add(InstInfo);
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">id</param>
        public static void DelInfo(int id)
        {
            Question.DelQuestionByAssess(id);
            Delete(id);
        }

        /// <summary>
        /// 根据关键字检查数据是否已存在
        /// </summary>
        /// <param name="keyword">关键字</param>
        /// <param name="editid">自己的ID</param>
        /// <returns></returns>
        public static bool CheckAssessExistByKeyword(string keyword, int editid)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return db.GetTable<Models.Assess>().Where(t => t.Keyword == keyword && t.Id != editid).Count() > 0;
            }
        }

        /// <summary>
        /// 根据名字检查数据是否已存在
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="keyword">关键字</param>
        /// <returns></returns>
        public static bool CheckAssessExistByName(int name,string keyword)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return db.GetTable<Models.Assess>().Where(t => t.Name == name || t.Keyword == keyword).Count() > 0;
            }
        }

        /// <summary>
        /// 根据关键字取得ID
        /// </summary>
        /// <param name="keyword">关键字</param>
        /// <returns></returns>
        public static int GetAssessIDByKeyword(string keyword)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                return db.GetTable<Models.Assess>().Single(t => t.Keyword == keyword).Id;
            }
        }
    }
}
